#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
根据源，统计数据条数量级（要支撑多个源同时统计）
'''

import sys

from apscheduler.schedulers.blocking import BlockingScheduler

from base import ExportAndTjBase
from models import MongoQueryConfig
from utils import Utils

reload(sys)
sys.setdefaultencoding('utf-8')


class ExportAndTj(ExportAndTjBase):
    def __init__(self, *args, **kwargs):
        super(ExportAndTj, self).__init__(*args, **kwargs)
        # todo other col
        # self.contact_col = self.db.p_col('contact')

    def handle_one_mongo_data(self, record_dic, save_result_dic=None):
        record_dic = Utils.decrypt_one_record(record_dic, need_decrypt=True, need_strf_time=True)
        # ["contact", "nameId", "name", "socialCreditCode", "regNumber", "contactType", "exportTime"]
        b2bSource = record_dic.get('b2bSource')

        must_exists_values = [b2bSource]  # 这些字段都必须是非空值
        must_has_one_exists_values = []  # 这些字段必须最少有一个是非空值
        if self.is_values_has_none(must_exists_values):
            self.write_error_f.write('not all exists: {}'.format(must_exists_values))
        elif self.is_values_all_none(must_has_one_exists_values):
            self.write_error_f.write('not any exists {}'.format(must_has_one_exists_values))
        else:
            if b2bSource not in save_result_dic:
                save_result_dic[b2bSource] = 0
            save_result_dic[b2bSource] += 1

        return save_result_dic

    def run(self):
        source_ls = [

        ]
        save_result_dic = {

        }

        config = {
            MongoQueryConfig.col_name: 'business',
            MongoQueryConfig.filter: {
                'b2bSource': {'$in': [source_ls]}

            },
            MongoQueryConfig.projection: ["b2bSource"]
        }
        result_dic = self.get_data_from_mongo(config, save_result_dic)


def run(abs_file_name):
    export_and_tj = ExportAndTj(abs_file_name)
    export_and_tj.main()


def scheduler_run(abs_file_name):
    scheduler = BlockingScheduler()
    scheduler.add_job(func=run, trigger='cron',
                      args=(abs_file_name,),
                      # day_of_week="sun",
                      hour='20', minute='52', id='dd_run')
    scheduler.start()


if __name__ == '__main__':
    file_name = 'business表多源不同量级统计'
    # py文件名+file_name+小时时间
    abs_file_name = Utils.generate_abs_path(file_name, current_py_path=__file__)

    run(abs_file_name)
    # scheduler_run(abs_file_name)
